/*
 * Copyright (C) 2021 The Chinese Software International Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tonyodev.fetch2.database;

import ohos.utils.Pair;

import com.tonyodev.fetch2.PrioritySort;
import com.tonyodev.fetch2.Status;
import com.tonyodev.fetch2core.Extras;
import com.tonyodev.fetch2core.Logger;

import java.io.IOException;
import java.util.List;

/**
 * @since 2021-05-24
 */
public class FetchDatabaseManagerWrapper implements FetchDatabaseManager {

    private FetchDatabaseManager fetchDatabaseManager;
    private Object lock;

    private FetchDatabaseManagerWrapper() {
    }

    public FetchDatabaseManagerWrapper(FetchDatabaseManager fetchDatabaseManager) {
        this.fetchDatabaseManager = fetchDatabaseManager;
        this.lock = new Object();
    }


    public boolean isClosed() {
        synchronized (lock) {
            return fetchDatabaseManager.isClosed();
        }
    }

    @Override
    public void setClosed(boolean isClosed) {
        synchronized (lock) {
            this.fetchDatabaseManager.setClosed(isClosed);
        }
    }

    @Override
    public Logger getLogger() {
        synchronized (lock) {
            return this.fetchDatabaseManager.getLogger();
        }
    }

    @Override
    public void setLogger(Logger logger) {
        synchronized (lock) {
            this.fetchDatabaseManager.setLogger(logger);
        }
    }

    public Delegate getDelegate() {
        synchronized (lock) {
            return fetchDatabaseManager.getDelegate();
        }
    }

    public void setDelegate(Delegate value) {
        synchronized (lock) {
            fetchDatabaseManager.setDelegate(value);
        }
    }


    @Override
    public Pair<DownloadInfo, Boolean> insert(DownloadInfo downloadInfo) {
        synchronized (lock) {
            return fetchDatabaseManager.insert(downloadInfo);
        }
    }

    @Override
    public List<Pair<DownloadInfo, Boolean>> insert(List<DownloadInfo> downloadInfoList) {
        synchronized (lock) {
            return fetchDatabaseManager.insert(downloadInfoList);
        }
    }

    @Override
    public void delete(DownloadInfo downloadInfo) {
        synchronized (lock) {
            fetchDatabaseManager.delete(downloadInfo);
        }
    }

    @Override
    public void delete(List<DownloadInfo> downloadInfoList) {
        synchronized (lock) {
            fetchDatabaseManager.delete(downloadInfoList);
        }
    }

    @Override
    public void deleteAll() {
        synchronized (lock) {
            fetchDatabaseManager.deleteAll();
        }
    }

    @Override
    public void update(DownloadInfo downloadInfo) {
        synchronized (lock) {
            fetchDatabaseManager.update(downloadInfo);
        }
    }

    @Override
    public void update(List<DownloadInfo> downloadInfoList) {
        synchronized (lock) {
            fetchDatabaseManager.update(downloadInfoList);
        }
    }

    @Override
    public void updateFileBytesInfoAndStatusOnly(DownloadInfo downloadInfo) {
        synchronized (lock) {
            fetchDatabaseManager.updateFileBytesInfoAndStatusOnly(downloadInfo);
        }
    }

    @Override
    public List<DownloadInfo> get() {
        synchronized (lock) {
            return fetchDatabaseManager.get();
        }
    }

    @Override
    public DownloadInfo get(int id) {
        synchronized (lock) {
            return fetchDatabaseManager.get(id);
        }
    }

    @Override
    public List<DownloadInfo> get(List<Integer> ids) {
        synchronized (lock) {
            return fetchDatabaseManager.get(ids);
        }
    }

    @Override
    public DownloadInfo getByFile(String file) {
        synchronized (lock) {
            return fetchDatabaseManager.getByFile(file);
        }
    }

    @Override
    public List<DownloadInfo> getByStatus(Status status) {
        synchronized (lock) {
            return fetchDatabaseManager.getByStatus(status);
        }
    }

    @Override
    public List<DownloadInfo> getByStatus(List<Status> statuses) {
        synchronized (lock) {
            return fetchDatabaseManager.getByStatus(statuses);
        }
    }

    @Override
    public List<DownloadInfo> getByGroup(int group) {
        synchronized (lock) {
            return fetchDatabaseManager.getByGroup(group);
        }
    }

    @Override
    public List<DownloadInfo> getDownloadsInGroupWithStatus(int groupId, List<Status> statuses) {
        synchronized (lock) {
            return fetchDatabaseManager.getDownloadsInGroupWithStatus(groupId, statuses);
        }
    }

    @Override
    public List<DownloadInfo> getDownloadsByRequestIdentifier(long identifier) {
        synchronized (lock) {
            return fetchDatabaseManager.getDownloadsByRequestIdentifier(identifier);
        }
    }

    @Override
    public List<DownloadInfo> getDownloadsByTag(String tag) {
        synchronized (lock) {
            return fetchDatabaseManager.getDownloadsByTag(tag);
        }
    }

    @Override
    public List<Integer> getAllGroupIds() {
        synchronized (lock) {
            return fetchDatabaseManager.getAllGroupIds();
        }
    }

    @Override
    public List<DownloadInfo> getPendingDownloadsSorted(PrioritySort prioritySort) {
        synchronized (lock) {
            return fetchDatabaseManager.getPendingDownloadsSorted(prioritySort);
        }
    }

    @Override
    public void sanitizeOnFirstEntry() {
        synchronized (lock) {
            fetchDatabaseManager.sanitizeOnFirstEntry();
        }
    }

    @Override
    public DownloadInfo updateExtras(int id, Extras extras) {
        synchronized (lock) {
            return fetchDatabaseManager.updateExtras(id, extras);
        }
    }

    @Override
    public long getPendingCount(boolean includeAddedDownloads) {
        synchronized (lock) {
            return fetchDatabaseManager.getPendingCount(includeAddedDownloads);
        }
    }

    @Override
    public DownloadInfo getNewDownloadInfoInstance() {
        synchronized (lock) {
            return fetchDatabaseManager.getNewDownloadInfoInstance();
        }
    }

    @Override
    public void close() throws IOException {
        synchronized (lock) {
            fetchDatabaseManager.close();
        }
    }

}
